<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Aug 1, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "html.HtmlControl" );

/**
 * The class for creating the Captcha image protect
 *
 */
class CaptchaImgProtect extends HtmlControl {
	
	function __construct($valueOrArrayContain, $inputName = "verify") {
		parent::__construct ( $inputName, $valueOrArrayContain );
	}
	
	/**
	 * Get the image URL for viewing
	 *	
	 * @param int $len
	 * @return String
	 */
	function getImageUrl($len = 10) {
		return SYS_ROOT_URL . "libraries/security/captcha/images/image.php?name={$this->_inputName}&len={$len}";
	}
	
	/**
	 * Create the HTML text for this control
	 *
	 * @param string $type input type: "text", "radio", "hidden",...
	 * @param string $extAttributes extend attributes of generated control
	 * @param string $id HTML id of this control
	 * 
	 * @return string HTML string
	 */
	function createInputHtml($extAttributes = "", $id = null) {
		SysResponse::addJavascriptFile ( "libraries/js/jquery.validate.min.js" );
		return parent::createInputHtml ( "text", $extAttributes, $id );
	}
	
	/**
	 * Get the image tag for viewing
	 *
	 * @param int $len
	 * @param string $extAttributes extend attributes of generated control
	 * @param string $id HTML id of this control
	 * 
	 * @return string HTML string
	 */
	function createImageTag($len = 10, $extAttributes = "", $id = null) {
		if (! empty ( $id )) {
			$id = "id=\"{$id}\"";
		}
		
		$captchaImg = $this->getImageUrl ( $len );
		return "<img  src=\"{$captchaImg}\" $extAttributes $id />";
	}
	
	/**
	 * Check if the picture and input is valid
	 *
	 * @return boolean
	 */
	function isValid() {
		return ($this->_value == $_SESSION ["{$this->_inputName}_capcha_string"]);
	}
}
?>